package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.R;
import com.example.entity.AddressBook;
import com.example.service.AddressBookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 地址管理(AddressBook)表控制层
 *
 * @author makejava
 * @since 2022-05-27 10:41:15
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {
    /**
     * 服务对象
     */
    @Resource
    private AddressBookService addressBookService;

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("list")
    public R<List<AddressBook>>addresslist(HttpSession session){
        Long userid= (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userid);
        List<AddressBook>list=addressBookService.list(queryWrapper);
        return R.success(list);
    }
    @PutMapping("/default")
    public R<String>putDefault(@RequestBody AddressBook addressBook,HttpSession session){
        Long userid= (Long) session.getAttribute("user");
        Long b=addressBook.getId();
        boolean a=addressBookService.updateServiceDefault(b,userid);
        if(a){
            return R.success("修改成功");
        }else{
            return R.error("修改失败");
        }

    }
    @PostMapping
    public R<String>postaddressBook(@RequestBody AddressBook addressBook,HttpSession session){
        addressBook.setIsDeleted(0);
        addressBook.setIsDefault(0);
        addressBook.setUserId((Long) session.getAttribute("user"));
        addressBookService.save(addressBook);
        return R.success("保存成功");
    }
    @GetMapping("/{id}")
    public R<AddressBook>getAddressBookbyid(@PathVariable Long id){
        AddressBook addressBook=addressBookService.getById(id);
        return R.success(addressBook);
    }
    @PutMapping
    public R<String>putAddressBook(@RequestBody AddressBook addressBook){
        boolean a=addressBookService.updateById(addressBook);
        if (a){
            return R.success("修改成功");
        }else{
            return R.error("修改失败");
        }
    }
    @DeleteMapping
    public R<String>deleteAddressBook(Long ids){
        boolean a=addressBookService.removeById(ids);
        if(a){
            return R.success("修改成功");
        }else{
            return R.error("未知错误");
        }
    }
    @GetMapping("/default")
    public R<AddressBook>getdefault(HttpSession session){
        LambdaQueryWrapper<AddressBook>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,session.getAttribute("user"));
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook=addressBookService.getOne(queryWrapper);
        return R.success(addressBook);
    }
}